package comment

import (
	"common/mildware"
	"common/models"
	"common/snowflake"
	"common/validate"
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"strconv"
	"strings"
	"video/pkg/dao/redis"
)

// AddVideoToComment 添加评论
// @Summary 添加评论
// @Description 添加评论
// @Tags 评论
// @Accept application/json
// @Produce application/json
// @Param Authorization header string true "Bearer 用户令牌"
// @Param object body docModel.DocAddVideCommentReq false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /comment/video/add [post]
func (h CHandler) AddVideoToComment(c *gin.Context) {
	var (
		commentId string
		//err error
		videoInfo                 *models.VideoInfo
		commentInfoP, commentInfo *models.CommentInfo
	)
	resp := new(models.RespData)
	req := new(models.AddVideCommentReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Common:AddVideoToComment with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Common:AddVideoToComment with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//直接读redis 获得视频的ID
	videoID, err := h.R.GetString(redis.VideIdToID + req.VideoId)
	if err != nil {
		if errors.Is(err, redis.NoKey) {
			//没有读到
			zap.L().Info("Comment: AddVideoToComment h.R.GetString error", zap.Error(err))
			//查mysql
			videoInfo, err = h.M.VideoInfoQueryByUid(req.VideoId)
			if err != nil {
				zap.L().Error("Comment: AddVideoToComment h.R.GetString error", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
		}
		zap.L().Error("Comment: AddVideoComment  h.R.GetString error", zap.Error(err))
	}
	if videoID != "" {
		videoInfo, err = h.M.VideoInfoQueryById(videoID)
		if err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.GetString error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
	}
	//获得ID
	ID, _, nickName, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("Comment: AddVideoToComment mildware.GetCyrrentUserID error", zap.Error(err))
		return
	}
	commentId = strconv.FormatInt(snowflake.GenID(), 10)
	commentInfo = &models.CommentInfo{
		CommentId:      commentId,
		Body:           req.Msg,
		ParentId:       models.SplitFormat,
		ChildIds:       models.SplitFormat,
		AuthorId:       ID,
		AuthorNickName: nickName,
		Weight:         2,
		VideoId:        strconv.Itoa(int(videoInfo.ID)),
	}
	//判断是否是一级评论
	if req.CommentId == models.SplitFormat {
		//创建评论
		if err = h.M.CommentInfoAdd(commentInfo); err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.CommentInfoAdd error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resComment, err := h.M.CommentInfoQueryByUid(commentInfo.CommentId)
		if err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.CommentInfoQueryByUid error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		commentID := strconv.Itoa(int(resComment.ID))
		//更新videoInfo
		//当是第一次添加时
		if videoInfo.CommentIds == "" {
			videoInfo.CommentIds = commentID
		} else {
			CommentIDList := strings.Split(videoInfo.CommentIds, models.SplitFormat)
			CommentIDList = append(CommentIDList, commentID)
			videoInfo.CommentIds = strings.Join(CommentIDList, models.SplitFormat)
		}
		if err := h.M.VideoInfoSaveById(videoInfo); err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.VideoInfoSaveByUid error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.SendSuccess(c)
		return
	} else {
		commentInfoP, err = h.M.CommentInfoQueryByUid(req.CommentId)
		if err != nil {
			zap.L().Error("Comment: AddVideoToComment h.M.CommentInfoQueryByUid error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		//判断本次评论的级别 -最多两级
		level := commentInfoP.Weight - 1
		if level < 0 {
			resp.SendCode(c, models.CodeNoCreateComment)
			return
		}
		//创建评论
		commentInfo.Weight = level
		commentInfo.ParentId = strconv.Itoa(int(commentInfoP.ID))
		if err = h.M.CommentInfoAdd(commentInfo); err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.CommentInfoAdd error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resComment, err := h.M.CommentInfoQueryByUid(commentInfo.CommentId)
		if err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.CommentInfoQueryByUid error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		commentID := strconv.Itoa(int(resComment.ID))
		//当是第一次添加时
		if commentInfoP.ChildIds == models.SplitFormat {
			commentInfoP.ChildIds = commentID
		} else {
			CommentINfoPList := strings.Split(commentInfoP.ChildIds, models.SplitFormat)
			CommentINfoPList = append(CommentINfoPList, commentID)
			//添加(父)子评论
			commentInfoP.ChildIds = strings.Join(CommentINfoPList, models.SplitFormat)
		}
		//更新父评论
		if err = h.M.CommentInfoSaveById(commentInfoP); err != nil {
			zap.L().Error("Comment: AddVideoToComment h.R.CommentInfoSaveById error", zap.Error(err))
			resp.SendServerBusy(c)
			return
		}
		resp.SendSuccess(c)
	}
}
